package org.hnxxxy.ebms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.mapper.RecruitStuMapper;
import org.hnxxxy.ebms.mapper.RoleMapper;
import org.hnxxxy.ebms.mapper.TeacherAdmissMapper;
import org.hnxxxy.ebms.pojo.Daliy;
import org.hnxxxy.ebms.mapper.DaliyMapper;
import org.hnxxxy.ebms.pojo.Plan;
import org.hnxxxy.ebms.pojo.Role;
import org.hnxxxy.ebms.pojo.TeacherAdmiss;
import org.hnxxxy.ebms.service.DaliyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hnxxxy.ebms.util.CommonUntil;
import org.hnxxxy.ebms.vo.DaliyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-10
 */
@Service
public class DaliyServiceImpl extends ServiceImpl<DaliyMapper, Daliy> implements DaliyService {

    @Autowired
    private DaliyMapper daliyMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private TeacherAdmissMapper teacherAdmissMapper;

    @Autowired
    private RecruitStuMapper recruitStuMapper;

    @Override
    public Result<IPage<DaliyVo>> getDaliy(int page) {
        IPage<DaliyVo> iPage = new Page<>(page,10);
        IPage<DaliyVo> daliyPage = null;
        // 查询当前用户是学生还是老师
        Role role = roleMapper.selectById(CommonUntil.getId());
        if (role.getIsTeacher()){
            // 学生
            daliyPage = daliyMapper.selectByRecStuId(iPage,role.getUserId());
        }else {
            // 老师
            // 通过老师id查询到地区id，
            List<Integer> admissId = teacherAdmissMapper.selectByTeaId(role.getUserId());
            // 通过地区id查询到学生id，
            List<Integer> recId = recruitStuMapper.selectByAdmissId(admissId);
            if (recId == null || recId.size() == 0){
                return Result.success(daliyPage);
            }
            // 根据学生id查询到学生的日报信息
            daliyPage = daliyMapper.selectByRecStuIds(iPage,recId);
        }
        return Result.success(daliyPage);
    }

    @Override
    public Result<String> add(Daliy daliy) {
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        daliy.setRecruitStuId(role.getUserId());
        this.save(daliy);
        return Result.success(200,"添加成功");
    }

    @Override
    public Result<IPage<DaliyVo>> getDaliyByName(String name, int page) {
        IPage<DaliyVo> iPage = new Page<>(page,10);
        Role role = roleMapper.selectById(CommonUntil.getId());
        // 通过老师id查询到地区id，
        List<Integer> admissId = teacherAdmissMapper.selectByTeaId(role.getUserId());
        // 通过地区id查询到学生id，
        List<Integer> recId = recruitStuMapper.selectByAdmissId(admissId);
        // 在根据学生id查询到姓名相符合的
        IPage<DaliyVo> daliyVoIPage = daliyMapper.selectByName(iPage,recId,name);
        return Result.success(daliyVoIPage);
    }

}
